package com.koro.utils;

import com.koro.annotation.*;
import org.springframework.util.StringUtils;

import java.lang.reflect.Field;

public class ColumnUtils {

	public static String getTableName(Class<?> clasz) {
		Table tableName = clasz.getAnnotation(Table.class);
		if (!hasTableAnnotation(clasz)) {
			return null;
		}
		if (tableName != null && !StringUtils.isEmpty(tableName.name())) {
			return tableName.name();
		}
		if (tableName != null && !StringUtils.isEmpty(tableName.value())) {
			return tableName.value();
		}
		throw new RuntimeException("表名称不能为空");
	}

	public static String getTableComment(Class<?> clasz) {
		Table table = clasz.getAnnotation(Table.class);
		TableComment tableComment = clasz.getAnnotation(TableComment.class);
		if (!hasTableAnnotation(clasz)) {
			return "";
		}
		if (table != null && !StringUtils.isEmpty(table.comment())) {
			return table.comment();
		}
		if (tableComment != null && !StringUtils.isEmpty(tableComment.value())) {
			return tableComment.value();
		}
		return "";
	}

	public static String getTableCharset(Class<?> clasz) {
		Table table = clasz.getAnnotation(Table.class);
		TableCharset charset = clasz.getAnnotation(TableCharset.class);
		if (!hasTableAnnotation(clasz)) {
			return "";
		}
		if (table != null && !StringUtils.isEmpty(table.charset())) {
			return table.charset();
		}
		if (charset != null && !StringUtils.isEmpty(charset.value())) {
			return charset.value();
		}
		return "";
	}

	public static String getTableEngine(Class<?> clasz) {
		Table table = clasz.getAnnotation(Table.class);
		TableEngine engine = clasz.getAnnotation(TableEngine.class);
		if (!hasTableAnnotation(clasz)) {
			return "";
		}
		if (table != null && !StringUtils.isEmpty(table.engine())) {
			return table.engine();
		}
		if (engine != null && !StringUtils.isEmpty(engine.value())) {
			return engine.value();
		}
		return "";
	}

	public static String getColumnName(Field field) {
		Column column = field.getAnnotation(Column.class);
		if (!hasColumnAnnotation(field)) {
			return null;
		}
		if (column != null && !StringUtils.isEmpty(column.name())) {
			return column.name().toLowerCase();
		}
		throw new RuntimeException("字段名称不能为空");
	}


	public static boolean isKey(Field field) {
		Column column = field.getAnnotation(Column.class);
		if (!hasColumnAnnotation(field)) {
			return false;
		}
		IsKey isKey = field.getAnnotation(IsKey.class);
		if (null != isKey) {
			return true;
		}

		if (column != null && column.isKey()) {
			return true;
		}
		return false;
	}

	public static boolean isAutoIncrement(Field field) {
		Column column = field.getAnnotation(Column.class);
		if (!hasColumnAnnotation(field)) {
			return false;
		}
		IsAutoIncrement isAutoIncrement = field.getAnnotation(IsAutoIncrement.class);
		if (null != isAutoIncrement) {
			return true;
		} else if (column != null && column.isAutoIncrement()) {
			return true;
		}
		return false;
	}

	public static boolean isNull(Field field) {
		Column column = field.getAnnotation(Column.class);
		if (!hasColumnAnnotation(field)) {
			return false;
		}
		IsNotNull isNotNull = field.getAnnotation(IsNotNull.class);
		if (null != isNotNull) {
			return false;
		} else if (column != null && column.isNull()) {
			return true;
		}
		return false;
	}

	public static String getComment(Field field) {
		Column column = field.getAnnotation(Column.class);
		ColumnComment comment = field.getAnnotation(ColumnComment.class);
		if (!hasColumnAnnotation(field)) {
			return null;
		}
		if (column != null && !StringUtils.isEmpty(column.comment())) {
			return column.comment();
		}
		if (comment != null && !StringUtils.isEmpty(comment.value())) {
			return comment.value();
		}
		return "";
	}

	public static String getDefaultValue(Field field) {
		Column column = field.getAnnotation(Column.class);
		DefaultValue defaultValue = field.getAnnotation(DefaultValue.class);
		if (!hasColumnAnnotation(field)) {
			return null;
		}
		if (column != null && !StringUtils.isEmpty(column.defaultValue())) {
			return column.defaultValue();
		}
		if (defaultValue != null && !StringUtils.isEmpty(defaultValue.value())) {
			return defaultValue.value();
		}
		return "NULL";
	}

	public static String getType(Field field) {
		Column column = field.getAnnotation(Column.class);
		ColumnType type = field.getAnnotation(ColumnType.class);
		if (!hasColumnAnnotation(field)) {
			return null;
		}
		if (column != null && !StringUtils.isEmpty(column.type())) {
			return column.type();
		}
		if (type != null && !StringUtils.isEmpty(type.value())) {
			return type.value();
		}
		return null;
	}

	public static boolean hasColumnAnnotation(Field field) {
		Column column = field.getAnnotation(Column.class);
		if (column == null) {
			return false;
		}
		return true;
	}

	public static boolean hasTableAnnotation(Class<?> clasz) {
		Table tableName = clasz.getAnnotation(Table.class);
		if (tableName == null) {
			return false;
		}
		return true;
	}
}
